//
//  GCMEQUErJiViewController.m
//  GeeCityManager
//
//  Created by 聚城科技 on 16/11/15.
//  Copyright © 2016年 聚城科技. All rights reserved.
//

#import "GCMEQUErJiViewController.h"
#import "GCMSheBeiRequst.h"
#import "GCMSBXQViewController.h"
#import "GCMSBCaoZuoViewController.h"
#import "GCMHistoryViewController.h"
#import "SubLBXScanViewController.h"
@interface GCMEQUErJiViewController ()

@end

@implementation GCMEQUErJiViewController{

    UILabel *_titleLab;
    UIView *view;
    UIButton *view1;
    NSArray * typeArr;
    NSMutableArray *_detailDic;
    UIButton *oldButton;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self setBackBar];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self downLoadData];
    [self creatrUI];
}

-(void)downLoadData{
    
    [GCMSheBeiRequst postScanDataSuccess:^(id obj, NSError *err) {
        typeArr  = obj;
        [self createScrollV];
    }];
    
    [GCMSheBeiRequst postBaseInfoWithsbId:self.sbid Success:^(id obj, NSError *err) {
        _detailDic = [[NSMutableArray alloc]init];
        _detailDic = obj;
//        NSLog(@"hahahahaahahah%@",_detailDic);
        [self createLabel];
    }];
}
-(void)createLabel{
    //    创建view上的label
    UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(5, 0, SCREEN_WIDTH - 20, 25)];
    label1.textColor = [UIColor lightGrayColor];
    [view1 addSubview:label1];
    UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(5, 25, SCREEN_WIDTH - 20, 25)];
    label2.textColor = [UIColor lightGrayColor];
    [view1 addSubview:label2];
    UILabel *label3 = [[UILabel alloc]initWithFrame:CGRectMake(5, 50, SCREEN_WIDTH - 20, 25)];
    label3.textColor = [UIColor lightGrayColor];
    [view1 addSubview:label3];
    UILabel *label4 = [[UILabel alloc]initWithFrame:CGRectMake(5, 75, SCREEN_WIDTH - 20, 25)];
    label4.textColor = [UIColor lightGrayColor];
    [view1 addSubview:label4];
    UILabel *label5 = [[UILabel alloc]initWithFrame:CGRectMake(5, 100, SCREEN_WIDTH - 20, 25)];
    label5.textColor = [UIColor lightGrayColor];
    [view1 addSubview:label5];
    UILabel *label6 = [[UILabel alloc]initWithFrame:CGRectMake(5, 125, SCREEN_WIDTH - 20, 25)];
    label6.textColor = [UIColor lightGrayColor];
    [view1 addSubview:label6];
 
    if ([_detailDic count] == 0) {
        
        [ProgressHUD showError:@"没有扫描相关设备的信息"];
        view1.userInteractionEnabled = NO;
        view1.userInteractionEnabled = NO;
        
    }else{
        label1.text = [NSString stringWithFormat:@"设备名称： %@",[_detailDic valueForKey:@"sbname"]];
        label2.text = [NSString stringWithFormat:@"状态： %@",[_detailDic valueForKey:@"zt"]];
        label3.text = [NSString stringWithFormat:@"位置： %@",[_detailDic valueForKey:@"azdd"]];
        label4.text = [NSString stringWithFormat:@"巡检计划： %@",[_detailDic valueForKey:@"period"]];
        label5.text = [NSString stringWithFormat:@"上次备注： %@",[_detailDic valueForKey:@"czqk"]];
        label6.text = [NSString stringWithFormat:@"上次时间： %@",[_detailDic valueForKey:@"createtime"]];
        _titleLab.text = [_detailDic valueForKey:@"sbname"];
    }
}
-(void)creatrUI{

    UIImageView *imageV  = [[UIImageView alloc]initWithFrame:CGRectMake(5, 70, SCREEN_WIDTH - 10, SCREEN_HEIGHT/4)];
    imageV.image = [UIImage imageNamed:@"tianqi"];
    [self.view addSubview:imageV];
    
    _titleLab  = [[UILabel alloc]initWithFrame:CGRectMake(10, SCREEN_HEIGHT/4 +80, (SCREEN_WIDTH  - 20)/2, 30)];
    _titleLab.text = self.sbname;
//    NSLog(@"%@",[_detailDic valueForKey:@"sbname"]);
//    _titleLab.backgroundColor = [UIColor redColor];
    [self.view addSubview:_titleLab];
    oldButton = [[UIButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH - 90, SCREEN_HEIGHT/4 +80, 80, 30)];
    oldButton.backgroundColor = [UIColor colorWithRed:0.25 green:0.44 blue:0.63 alpha:1];
    oldButton.layer.cornerRadius = 3;
    [oldButton setTitle:@"历史记录" forState:UIControlStateNormal];
    [oldButton addTarget:self action:@selector(oldButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:oldButton];
    
    view = [[UIView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT/4 +120, SCREEN_WIDTH, 160)];
    view.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1.0];
    [self.view addSubview:view];
    view1 = [[UIButton alloc]initWithFrame:CGRectMake(5, 5, SCREEN_WIDTH - 10, 150)];
    view1.backgroundColor = [UIColor whiteColor];
    [view1 addTarget:self action:@selector(onbuttonClick) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:view1];
    

}
-(void)oldButtonClick{
    GCMHistoryViewController *sbxqVC  = [[GCMHistoryViewController alloc]init];
//    sbxqVC.type = self.type;
    sbxqVC.sbid = self.sbid;
    [self.navigationController pushViewController:sbxqVC animated:NO];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] init];
    backItem.title = @"历史";
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    self.navigationItem.backBarButtonItem = backItem;
    
}
-(void)createScrollV{
    NSArray * array = @[@"14",@"30",@"8",@"13",@"14",@"30",@"8",@"13",@"14",@"30",@"8",@"13",@"14",@"30",@"8",@"13"];
    UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT - SCREEN_WIDTH/4, SCREEN_WIDTH, SCREEN_WIDTH/4)];
    scrollView.showsVerticalScrollIndicator = FALSE;
    scrollView.showsHorizontalScrollIndicator = FALSE;
    scrollView.contentSize = CGSizeMake(SCREEN_WIDTH/4*typeArr.count, 0);
    for (int i=0; i<typeArr.count; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(SCREEN_WIDTH/4*i, 0, SCREEN_WIDTH/4, SCREEN_WIDTH/4);
        button.tag = 6446+i;
        [button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
//        [button setTitle:typeArr[i] forState:UIControlStateNormal];
        UILabel *lab = [[UILabel alloc]initWithFrame:CGRectMake(0, SCREEN_WIDTH/4-30, SCREEN_WIDTH/4, 30)];
        lab.text = typeArr[i];
        lab.tag = 6446+i +1;
        lab.textColor = [UIColor lightGrayColor];
        lab.textAlignment = NSTextAlignmentCenter;
        [button addSubview:lab];
        UIImageView *imageV = [[UIImageView alloc]initWithFrame:CGRectMake(15, 0, SCREEN_WIDTH/4-30, SCREEN_WIDTH/4-30)];
        imageV.image = [UIImage imageNamed:array[i]];
        [button addSubview:imageV];
        [scrollView addSubview:button];
    }
    [self.view addSubview:scrollView];
}

-(void)onbuttonClick{

    GCMSBXQViewController *sbxqVC  = [[GCMSBXQViewController alloc]init];
    sbxqVC.type = self.type;
    sbxqVC.sbid = self.sbid;
    [self.navigationController pushViewController:sbxqVC animated:NO];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] init];
    backItem.title = @"详细信息";
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    self.navigationItem.backBarButtonItem = backItem;
    
}

-(void)click:(UIButton*)sender{
    
    UILabel *lab =(UILabel*)[self.view viewWithTag:(sender.tag +1)];
    GCMSBCaoZuoViewController *caozuoVC = [[GCMSBCaoZuoViewController alloc]init];
    caozuoVC.type = lab.text;
    caozuoVC.sbid = self.sbid;
    [self.navigationController pushViewController:caozuoVC animated:NO];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] init];
    backItem.title = @"返回";
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    self.navigationItem.backBarButtonItem = backItem;
}

//重写返回方法
-(void)setBackBar{
    
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(0, 0, 65, 44);
    
    [backBtn setImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];
    [backBtn setTitle:@"返回" forState:UIControlStateNormal ];
    [backBtn addTarget:self action:@selector(doBack:) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    self.navigationItem.leftBarButtonItem = backItem;
    
}
-(void)doBack:(id)sender
{
    NSMutableArray *marr = [[NSMutableArray alloc]initWithArray:self.navigationController.viewControllers];
    for (UIViewController *vc in marr) {
        if ([vc isKindOfClass:[SubLBXScanViewController class]]) {
            [marr removeObject:vc];
            break;
        }
    }
    self.navigationController.viewControllers = marr;
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
